#include "head_s.h"
int create_dir(const user_t* status, const portocol_t* portocol, int net_fd, MYSQL *mysql) {

    int n = 0;
    int i = 0;
    int size = strlen(status->arg);
    if(size == 0){
        send(net_fd, "failed", 6, 0);
        return 0;
    }
    while(status->arg[i] != '\0') {
        if((status->arg[i] == ' ' ||
            status->arg[i] == '"' ||
            status->arg[i] == '*' ||
            status->arg[i] == '?' ||
            status->arg[i] == ':')   ||
           (status->arg[i] == '/' && status->arg[i+1] == '/') || (status->arg[0] == '/')){
            send(net_fd, "failed", 6, 0);
            return 0;
        }
        if(status->arg[i] == '/') {
            n++;
        }
        i++;
    }

    if(n > 1) {
        send(net_fd, "failed", 6, 0);
        return 0;
    }
    //目录名处理
    char file[128] = {0};
    sprintf(file, "%s", status->arg);

    if(file[i-1] == '/'){
        file[i-1] = '\0';
    }


    //拼接目标目录路径
    char path[1024] = {0};
    strncpy(path, status->path, sizeof(status->path));
    char file_path[124] = {0};
    sprintf(file_path, "%s%s", path, file);

    //处理当前工作目录路径
    char dir_path[124] ={0};
    int len = strlen(status->path);
    sprintf(dir_path, "%s", status->path);
    if(len != 1){
        dir_path[len - 1] = '\0';
    }
    /*
    //多级目录创建  （待做）
    char *delimiter = "/";
    char *token;
    token = strtok(status->arg, delimiter);

    int n = 0;
    if(status->arg[0] == '/') {
    while(token != NULL) {

    //判断token 在当前目录是否存在
    //1.若存在且下一个token为NULL --> 返回客户端 “已存在”
    //2.若存在且下一个token不为NULL --> 遍历下一个token
    //3.若不存在则mkdir
    char buf[256] = {0};
    mysql_query(mysql, "");


    token = strtok(NULL, delimiter);
    }
    return 0;
    }*/

    //获取user_id
    char user_id[10] = {0};
    char buf1[1024] = {0};
    sprintf(buf1, "select id from user_info where name ='%s'", status->user_name);

    mysql_query(mysql, buf1);
    MYSQL_RES *res1 = mysql_store_result(mysql);
    MYSQL_ROW row1 = mysql_fetch_row(res1);
    strncpy(user_id, row1[0], sizeof(user_id)-1);
    mysql_free_result(res1);
    int u_id = atoi(user_id);

    //获取当前工作目录id
    char buf2[1024] = {0};
    char p_id[10] = {0};
    sprintf(buf2, "select id from file_info where user_id = %d and file_path = '%s' and file_type = 1", u_id, dir_path);

    mysql_query(mysql, buf2);
    MYSQL_RES *res2 = mysql_store_result(mysql);
    MYSQL_ROW row2 = mysql_fetch_row(res2);
    strncpy(p_id, row2[0], sizeof(row2[0]) -1);
    mysql_free_result(res2);


    int pid = atoi(p_id);
    //查找目标是否存在
    char buf3[1024] = {0};
    sprintf(buf3, "select * from file_info where user_id = %d and file_path = '%s' and file_type = 1 and exist_flag = 0", u_id, file_path);

    mysql_query(mysql, buf3);

    MYSQL_RES *res3 = mysql_store_result(mysql);
    if (res3 != NULL && (mysql_num_rows(res3)) > 0)
    {
        LOG_WRITE(LOG_INFO, "该目录下有同名文件夹");
        send(net_fd, "already exists.", 15, MSG_NOSIGNAL);
        mysql_free_result(res3);
        return 0;
    }


    mysql_free_result(res3);

    LOG_WRITE(LOG_INFO, "无同名文件夹");

    char buf4[1024] = {0};
    sprintf(buf4, "insert into file_info values(NULL, '%s', %d, %d, '%s', 1, NULL, 0)", file, u_id, pid, file_path);
    mysql_query(mysql, buf4);
    //添加数据成功
    send(net_fd, "succeeded", 9, 0);
    LOG_WRITE(LOG_INFO, "添加数据成功");

    return 0;
}
